<?
		include("dbconn.inc");
		include("dbfuc.inc");
		include("session.inc");		
		$randam= rand(5, 100000);
		$_SESSION["session_pointtransfer_random"]=$randam;
		$adminuser=$_SESSION["adminuser"];
		$adminid=$_SESSION["adminid"];
		$admindb=$_SESSION["admindb"];
		
	if(session_is_registered("admindb") )
	{
		if(($adminuser!="") && (($adminid!="") || ($adminid!=0)))
		{	
	


	$sql="";$slno="";$tuser="";$tpass="";$stat="";
	$flag=0;$flag2=0;$flag3=0;$errcount=0;$ercnt=0;
	$from=$HTTP_POST_VARS["from"];
	$to=$HTTP_POST_VARS["to"];	
	$issue_user=$HTTP_POST_VARS["textusername"];
	$userflag="false";
	
	if($from=="" || $to=="")
	{
		$ercnt=$ercnt+1;
		$ermsg="Select a Coupon Range";

	}
	else
	{			if(checkString($transfer_points))  {
		if(checkString($from) || checkString($to))
		{
			$ercnt=$ercnt+1;
			$ermsg="Invalid Enteries";
		}
		else if((checkInt($from))||(checkInt($to)))  
		{
			$ercnt=$ercnt+1;
			$ermsg="Invalid Enteries (Only Digits)";
		}
	else if($from > $to)
	{
		$ercnt=$ercnt+1;
	 	$ermsg="'FROM' Entry Should be less than or equal to 'TO' entry";
		
	}
	else
	{ 
			$sql="select count(1) count from mlm_coupon_details where N_SLNO between '$from' and '$to' and c_status='N' ";
			$result=mysql_query($sql);
			$rownum=mysql_num_rows($result);
			if($rownum >0)
			{
				$rownum=$rownum-1;
				$cpncnt=mysql_result($result,$rownum,"count");			
			}		
		if($cpncnt<=0)
		{
			$ercnt=$ercnt+1;
	 		$ermsg="Invalid Range (You are not allowed to coupons in this Range ie. count is 0)";
		}
		
	}
	
	}	
	if($issue_user=="")
	{
		$ercnt=$ercnt+1;
		$ermsg="Invalid username ";
		echo $ercnt;
		
	}
	else
	{	
		$sql="select pn_id from mlm_master where c_username='$issue_user' ";
		$result=mysql_query($sql);
		$rownum=mysql_num_rows($result);
		if($rownum >0)
		{
			$rownum=$rownum-1;
			$coupon_userid=mysql_result($result,$rownum,"pn_id");		
			$userflag="true";	
		}
		if($userflag=="false")
		{
			$ercnt=$ercnt+1;
			$ermsg="Invalid username ";
		}
	}
	}
?>
<html>
<head>
<title>coupons</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function Check()
{
	if(document.myform.Check_All.value=="Check All")
	{
		for(i=0; i<document.myform.elements.length; i++)
		{
			if(document.myform.elements[i].type=="checkbox")
			document.myform.elements[i].checked=true;
		}
		document.myform.Check_All.value="UnCheck All";
	}
	else
	{
		for(i=0; i<document.myform.elements.length; i++)
		{
			if(document.myform.elements[i].type=="checkbox")
				document.myform.elements[i].checked=false;
		}
		document.myform.Check_All.value="Check All";
	}
}
// End -->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT TYPE="text/javascript" LANGUAGE="javascript">
<!-- PreLoad Wait - Script -->
<!-- This script and more from http://www.rainbow.arch.scriptmania.com 

function waitPreloadPage() { //DOM
if (document.getElementById){
document.getElementById('prepage').style.visibility='hidden';
}else{
if (document.layers){ //NS4
document.prepage.visibility = 'hidden';
}
else { //IE4
document.all.prepage.style.visibility = 'hidden';
}
}
}
// End -->
</SCRIPT>
</head>

<body>
<? include "allheader.php"  ?>	
<?
if($ercnt>0)
{
	echo "<h4>Error<br><hr width='20%' align='left'><br>&nbsp;&nbsp;&nbsp;".$ermsg."<h4>";
}
else
{

?>

<form name="myform" action="coupon_insert.php" method="post">
<br>
<table width="66%" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#659931" bgcolor="#F8FDE3">
<tr bordercolor="#FF0000" bgcolor="#CCCC00">
    <td colspan="4" bgcolor="#659931">
	<div align="center">
	<strong>
	<font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Coupen Allotment for the - <? echo $issue_user ?>
	</font>
	</strong>
	</div>
	</td>
</tr>
<tr bordercolor="#659931" bgcolor="#BDDF9C"> 
    <td width="21%">
	<div align="center"><strong><input type="button" name="Check_All" value="Check All" onClick="Check()"></strong></div>
	</td>
    <td width="20%">
	<div align="center"><strong><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Sl:No</font></strong></div>
	</td>
    <td width="26%">
	<div align="center">
	<strong><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Coupon User</font></strong>
	</div>
	</td>
    <td width="33%">
	<div align="center"><strong><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Coupon Password</font></strong></div>
	</td>
</tr>
<?
			$sql="select N_SLNO,C_COUPON_NAME,C_COUPON_PASSWORD,C_STATUS,n_id from mlm_coupon_details where N_SLNO between '$from' and '$to' order by N_SLNO";
			$result=mysql_query($sql);
			$rownum=mysql_num_rows($result);
			while($rownum >0)
			{
				$rownum=$rownum-1;
				$slno=mysql_result($result,$rownum,"N_SLNO");
				$couponuser=mysql_result($result,$rownum,"C_COUPON_NAME");			
				$couponpassword=mysql_result($result,$rownum,"C_COUPON_PASSWORD");
				$stat=mysql_result($result,$rownum,"C_STATUS");
				$issueduserid=mysql_result($result,$rownum,"n_id");
				$flag2=$flag2+1;

				if($stat=="Y")
					$flag=1;
				else
					$flag=0;
				if($issueduserid!=0)
					$flag3=1;
				else
					$flag3=0;					
?>
<tr bgcolor="#F8FDE3"> 
<?
  		if($flag==1)
  		{
?>
  			<td>
			<div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"><font size="2">Used</font></font></div>
	  </td>
<?
  		}
		else if($flag3==1)
		{
?>
  			<td>
			<div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"><font size="2">Issued coupon</font></font></div>
	  </td>
<?
  		}
  		else
 		{
?>
		    <td>
			<div align="center">
			<input name="chk<? echo $flag2 ?>" type="checkbox" value="Y">
			<input type="hidden" name="slno<? echo $flag2 ?>" value="<? echo $slno ?>">
			</div>
	  </td>
<?
   		}
?>
 
	    <td>
		<div align="center"><font size="2" face="Geneva, Arial, Helvetica, sans-serif"><? echo $slno ?></font></div>
	  </td>
      <td>
		<div align="center"><font size="2" face="Geneva, Arial, Helvetica, sans-serif"><? echo $couponuser ?></font></div>
	  </td>
	  <td>
		<div align="center"><font size="2" face="Geneva, Arial, Helvetica, sans-serif"><? echo $couponpassword ?></font></div>
	  </td>
</tr>
<?
	}

	if($flag2==0)
	{
?>
		<tr bgcolor="#F8FDE3"> 
    		<td colspan="4">
			<div align="center">
			<font color="#FF0000"><strong><font size="2" face="Lucida Sans Unicode">No Records .... </font></strong></font>
			</div>
		  </td>
  		</tr>
<?
	}
	else
	{
?>
		<tr bgcolor="#F8FDE3"> 
    		<td colspan="4"><div align="center"> 
          	<input type="submit" name="Submit" value="Submit">
		   	<input type="hidden" name="cnt" value="<? echo $flag2 ?>">
		   	<input type="hidden" name="coupon_userid" value="<? echo $coupon_userid ?>">
	      </div></td>
  		</tr>
<?
	}
?>
</table>
<br><br><br><br><br><br><br><br>
</form>
<?
}//errorcount is zero
?>
</body>
</html>
<?
	
	}
	}
	else
	{
		$errormsg="Your session is expeired. Please login again.";
		header("location: index.php?error=$errormsg");
	}
?>